package com.example.dllo.my36kr.ui.activity.sonactivity;

import android.content.Context;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.RadioButton;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.EventBean;
import com.example.dllo.my36kr.net.SingleVolley;
import com.example.dllo.my36kr.net.VolleyResult;
import com.example.dllo.my36kr.tools.EventUrls;
import com.example.dllo.my36kr.ui.activity.AbsBaseActivity;
import com.example.dllo.my36kr.ui.adapter.EventAdapter;
import com.google.gson.Gson;

import java.util.List;

/**
 * 近期活动
 * Created by CPC_272 on 16/8/23.
 */
public class EventActivity extends AbsBaseActivity implements View.OnClickListener, VolleyResult, PopupMenu.OnMenuItemClickListener {
    private ListView ls_event;
    private ImageView image_title_back;
    private RadioButton RB_first, RB_second;
    private String replaceUrl;
    @Override
    protected int setLayout() {
        return R.layout.activity_event;
    }

    @Override
    protected void initView() {
        ls_event = byView(R.id.ls_envent);
        RB_first = byView(R.id.RB_One);
        RB_second = byView(R.id.RB_Two);
        image_title_back = byView(R.id.event_back);
    }

    @Override
    protected void initData() {
        RB_first.setOnClickListener(this);
        RB_second.setOnClickListener(this);
        image_title_back.setOnClickListener(this);
        SingleVolley.getInstence(this).startRequest(EventUrls.ALL_URL,this,1);
          ls_event.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  EventBean.DataBean.DataBean2 dataBean2 = (EventBean.DataBean.DataBean2) parent.getItemAtPosition(position);
                  Intent intent = new Intent(EventActivity.this, EventAllMinuteActivity.class);
                  intent.putExtra("intentURL", dataBean2.getActivityLink());
                  startActivity(intent);
              }
          });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.RB_One:
                PopupMenu menu = new PopupMenu(EventActivity.this, view); //建立PopupMenu对象
                //调用MenuInflater的inflate()方法将菜单资源填充到PopupMenu中。
                menu.getMenuInflater().inflate(R.menu.menu_first, menu.getMenu());

                menu.setOnMenuItemClickListener(EventActivity.this); //菜单每行的监听事件
                //调用PopupMenu的show()方法显示弹出式菜单。
                menu.show();
                break;
            case R.id.RB_Two:
                // 用它的构造器实例化一个PopupMenu对象，它需要当前应用程序的Context和菜单要停靠的那个View两个对象作为参数。
                PopupMenu menuTime = new PopupMenu(EventActivity.this, view);
                //使用MenuInflater对象把你的菜单资源装载到由PopupMenu.getMenu()方法返回的Menu对象中。在API 级别 14以上的版本，你能够使用PopupMenu.inflate()方法来替代。
                menuTime.getMenuInflater().inflate(R.menu.menu_second, menuTime.getMenu());
                menuTime.setOnMenuItemClickListener(EventActivity.this);
                menuTime.show();
                break;
            case R.id.event_back:
                finish();
                break;
        }
    }

    @Override
    public void success(String str, int type) {
        switch (type) {
            case 1:
                Gson eventGson = new Gson();
                EventBean dataBean = eventGson.fromJson(str, EventBean.class);
                List<EventBean.DataBean.DataBean2> eventList = dataBean.getData().getData();
                EventAdapter eventAdapter = new EventAdapter(EventActivity.this);
                eventAdapter.setEventBeen(eventList);
                ls_event.setAdapter(eventAdapter);
                break;

            case 2:
                Gson eventGson2 = new Gson();
                EventBean dataBean2 = eventGson2.fromJson(str, EventBean.class);
                List<EventBean.DataBean.DataBean2> eventList2 = dataBean2.getData().getData();
                EventAdapter eventAdapter2 = new EventAdapter(EventActivity.this);
                eventAdapter2.setEventBeen(eventList2);
                ls_event.setAdapter(eventAdapter2);
                break;
        }

    }

    @Override
    public void success(String str) {

    }

    @Override
    public void failure() {

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_one://全部
                replaceUrl = EventUrls.ALL_URL;
                replaceData();
                break;
            case R.id.item_two://"Demo Day
                replaceUrl = EventUrls.DEMODAY_URL;
                replaceData();
                break;
            case R.id.item_three://氪空间
                replaceUrl = EventUrls.KRSPACE_URL;
                replaceData();
                break;
            case R.id.item_forth://股权投资
                replaceUrl = EventUrls.INVEST_URL;
                replaceData();
                break;
            case R.id.item_five://企业服务
                replaceUrl = EventUrls.SEVEICE_URL;
                replaceData();
                break;
            case R.id.item_six://极速融资
                replaceUrl = EventUrls.TOPSPEED_URL;
                replaceData();
                break;
            case R.id.item_time_one://全部时间
                replaceUrl= EventUrls.ALL_URL;
                replaceData();
                break;

        }
        return false;
    }
    public void replaceData() {
        SingleVolley.getInstence(this).startRequest(replaceUrl, this, 2);
    }
}
